package org.zxl.api.common.util;

import org.zxl.api.common.PlatformInfoCache;
import org.zxl.api.common.dto.CloudRequestEntity;
import org.zxl.api.common.funcInterface.CheckAuthFailExceptionHandler;
import org.zxl.api.common.funcInterface.HttpHeaderBuilder;
import org.zxl.api.common.funcInterface.HttpResponseParse;
import org.zxl.api.platform.dto.LoginReqDTO;

/**
 * @author zhangxiaolong
 * @date 2025-02-17 17:56
 **/
public class HttpClient {

    public <T> T get(String platformUuid, String url, HttpHeaderBuilder httpHeader, HttpResponseParse httpResponseParse, CheckAuthFailExceptionHandler checkAuthFailExceptionHandler, Class<T> resultClass, String reqBody) {
        String response = "";
        Exception exception = null;
        try {
            String s = httpHeader.buildHttpHeader("");
            // http请求
            response = "{'name':'vm-01','ip':'192.168.11.108'}";
            return httpResponseParse.parse(response, resultClass);
        } catch (Exception e) {
            exception = e;
            throw e;
        }finally {
            boolean authFail = checkAuthFailExceptionHandler.fail(exception, response);
            if(authFail){
                //清空authInfo缓存
                PlatformInfoCache.clear(platformUuid);
                //触发登录
                CloudRequestEntity<LoginReqDTO> loginReqEntity = PlatformInfoCache.getLoginReqEntity(platformUuid);
                PlatformInfoCache.getPlatformService(platformUuid).login(loginReqEntity);
            }
        }
    }

    public String post(HttpHeaderBuilder httpHeader, String body) {
        return "HttpClient post";
    }

    public String put(HttpHeaderBuilder httpHeader, String body) {
        return "HttpClient put";
    }

    public String delete(HttpHeaderBuilder httpHeader, String body) {
        return "HttpClient delete";
    }
}
